﻿using System;
using Autodesk.Revit.DB;

namespace FetchDataCompare
{
	internal class Utlity
	{
		public static GraphicsStyle GetLineStyle(Document doc, string strLineTypeName, Color color, int iLineWeight = 5)
		{
			Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
			if (category == null)
			{
				return null;
			}
			if (!category.SubCategories.Contains(strLineTypeName))
			{
				Transaction transaction = new Transaction(doc, "new line style");
				try
				{
					transaction.Start();
					Category category2 = doc.Settings.Categories.NewSubcategory(category, strLineTypeName);
					category2.LineColor = color;
					category2.SetLineWeight(iLineWeight, (Autodesk.Revit.DB.GraphicsStyleType)1);
					transaction.Commit();
				}
				catch (Exception)
				{
					if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
					{
						transaction.RollBack();
					}
					return null;
				}
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(GraphicsStyle));
			foreach (Element element in filteredElementCollector)
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				if (graphicsStyle != null && graphicsStyle.Name.CompareTo(strLineTypeName) == 0)
				{
					return graphicsStyle;
				}
			}
			return null;
		}
	}
}
